<template>
	<div class="app-container">

		<el-card>
			<el-tabs :tab-position="tabPosition" v-model="activeName" @tab-click="tabClick">
				<el-tab-pane v-for="(item,index) in arr" :key="index" :label="item" :name="`tab-${index}`">


					<div v-if="ifShow==true" style="padding:15px">
						<div class="top">{{item}}</div>
						<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
							label-width="68px">
							<el-form-item label="订单编号" prop="number">
								<el-input v-model="queryParams.number" placeholder="请输入订单编号" clearable
									@keyup.enter.native="handleQuery" />
							</el-form-item>
							<el-form-item label="预约时间" prop="subscribeTime">
								<el-date-picker clearable v-model="queryParams.subscribeTime" type="date"
									value-format="yyyy-MM-dd" placeholder="请选择预约时间">
								</el-date-picker>
							</el-form-item>
							<el-form-item label="下单时间" prop="createTime">
								<el-date-picker clearable v-model="queryParams.createTime" type="date"
									value-format="yyyy-MM-dd" placeholder="请选择下单时间">
								</el-date-picker>
							</el-form-item>
							<el-form-item label="状态" prop="state">
								<el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
									<el-option v-for="dict in dict.type.cf_order_state" :key="dict.value"
										:label="dict.label" :value="dict.value" />
								</el-select>
							</el-form-item>
							<el-form-item>
								<el-button type="primary" icon="el-icon-search" size="mini"
									@click="handleQuery">搜索</el-button>
								<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

								<el-button type="warning" plain icon="el-icon-download" size="mini"
									@click="handleExport" v-hasPermi="['order:order:export']">导出</el-button>
							</el-form-item>
						</el-form>

						<el-table  :data="orderList">
							<el-table-column label="订单编号" align="center" prop="id" />
							<el-table-column label="服务编号" align="center" prop="serviceId" />
							<el-table-column label="服务名称" align="center" prop="serviceName" />
							<el-table-column label="买家" align="center">
								<template slot-scope="scope">
									<div>{{scope.row.username}}</div>
									<div>{{scope.row.phone}}</div>
								</template>
							</el-table-column>
							<el-table-column label="商家" align="center" prop="shopName" />
							<el-table-column label="价格" align="center">
								<template slot-scope="scope">
									<div>原价：{{scope.row.totalPrice}}元</div>
									<div>优惠：{{scope.row.totalPrice-scope.row.realPrice}}元</div>
									<div>实付：{{scope.row.realPrice}}元</div>
								</template>
							</el-table-column>
							<el-table-column label="状态" align="center">
								<template slot-scope="scope">
									<span v-if="scope.row.state==0" >待支付</span>
									<span v-if="scope.row.state==1" >待使用</span>
									<span v-if="scope.row.state==2" >待评价</span>
									<span v-if="scope.row.state==3" >已评价</span>
									<span v-if="scope.row.state==4" >已取消</span>
									<span v-if="scope.row.state==5" >已退款</span>
								</template>
							</el-table-column>

							<el-table-column label="预约时间" align="center" prop="subscribeTime" width="180">
								<template slot-scope="scope">
									<span>{{ parseTime(scope.row.subscribeTime, '{y}-{m}-{d}') }}</span>
								</template>
							</el-table-column>
							<el-table-column label="下单时间" align="center" prop="createTime" width="180">
								<template slot-scope="scope">
									<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
								</template>
							</el-table-column>

							<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
								<template slot-scope="scope">
									<el-button size="mini" type="text" icon="el-icon-edit" @click="xq(scope.row)"
										>详情</el-button>
								</template>
							</el-table-column>
						</el-table>

						<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
							:limit.sync="queryParams.pageSize" @pagination="getList" />
					</div>

					<div v-else style="padding:15px">
						<div style="display:flex; justify-content: space-between;border-bottom:1px solid #BDBDBD">
							<div>订单详情</div>
							<el-button class="back" @click="back" type="primary">返回</el-button>
						</div>

						<div class="total">
							<div class="tt">
								<div class="bc">
									订单编号：
								</div>
								<div class="nobc">
									{{xqData.id}}
								</div>
								<div class="bc">
									服务编号
								</div>
								<div class="nobc">
									{{xqData.serviceId}}
								</div>
							</div>

							<div class="tt">
								<div class="bc">
									核销码：
								</div>
								<div class="nobc">
									{{xqData.verificationCode}}
								</div>
								<div class="bc">
									商家名称：
								</div>
								<div class="nobc">
									{{xqData.shopName}}
								</div>
							</div>


							<div class="tt">
								<div class="bc">
									买家：
								</div>
								<div class="nobc">
									{{xqData.username}}
								</div>
								<div class="bc">
									订单状态：
								</div>
								<div v-if="xqData.state==0" class="nobc">
									待支付
								</div>
								<div v-if="xqData.state==1" class="nobc">
									待使用
								</div>
								<div v-if="xqData.state==2" class="nobc">
									待评价
								</div>
								<div v-if="xqData.state==3" class="nobc">
									已评价
								</div>
								<div v-if="xqData.state==4" class="nobc">
									已取消
								</div>
								<div v-if="xqData.state==5" class="nobc">
									已退款
								</div>
							</div>


							<div class="tt">
								<div class="bc">
									下单时间：
								</div>
								<div class="nobc">
									{{xqData.createTime}}
								</div>
								<div class="bc">
									预约时间：
								</div>
								<div class="nobc">
									{{xqData.subscribeTime}}
								</div>
							</div>

              <div class="tt">
                <div class="bc">
                  原价：
                </div>
                <div class="nobac">
									{{xqData.totalPrice}}
                </div>
              </div>

              <div class="tt">
                <div class="bc">
                  优惠：
                </div>
                <div class="nobac">
									{{xqData.totalPrice-xqData.realPrice}}
                </div>
              </div>

              <div class="tt">
                <div class="bc">
                  实付金额：
                </div>
                <div class="nobac">
									{{xqData.realPrice}}
                </div>
              </div>

              <div class="tt">
                <div class="bc">
                  服务内容：
                </div>
                <div class="nobac">
									{{xqData.serviceName}}
                </div>
              </div>


						</div>
					</div>
				</el-tab-pane>
			</el-tabs>
		</el-card>

	</div>
</template>

<script>
	import {
		listOrder,
		getOrder,
		delOrder,
		addOrder,
		updateOrder
	} from "@/api/order/order";

	export default {
		name: "Order",
		dicts: ['cf_order_state'],
		data() {
			return {
				activeName: 'tab-0',
				xqData:{},
				ifShow: true,
				arr: ["洗车订单", "维修订单", "保养订单"],
				tabPosition: 'left',
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 订单管理表格数据
				orderList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					serviceId:1,
					pageNum: 1,
					pageSize: 10,
					number: null,
					subscribeTime: null,
					createTime: null,
					state: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询订单管理列表 */
			getList() {
				if(this.activeName=='tab-0'){
					this.queryParams.serviceId=3

				}
				listOrder(this.queryParams).then(response => {
					console.log(response.rows);
					this.orderList = response.rows;
					this.total = response.total;

				});

			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					number: null,
					shopId: null,
					carId: null,
					userId: null,
					serviceId: null,
					couponId: null,
					remark: null,
					totalPrice: null,
					realPrice: null,
					verificationCode: null,
					subscribeTime: null,
					createTime: null,
					verificationTime: null,
					state: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},

			tabClick(tab, event) {
      const index = parseInt(tab.name.replace('tab-', ''));
      console.log('Clicked tab index:', index);
			if(index==1){
				this.queryParams.serviceId=1
				this.getList()
			}else if(index==2){
				this.queryParams.serviceId=2
				this.getList()
			}else{
				this.queryParams.serviceId=3
				this.getList()
			}

    },


			xq(row) {
				this.ifShow = false
				console.log(row);
				this.xqData = row
			},
			back() {
				this.ifShow = true
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateOrder(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addOrder(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除订单管理编号为"' + ids + '"的数据项？').then(function() {
					return delOrder(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('order/order/export', {
					...this.queryParams
				}, `order_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>

<style scoped>
	.top {
		height: 50px;
		margin-bottom: 30px;
		font-size: 14px;
		background-color: #F5F5F5;
		line-height: 50px;

	}

	.back {
		margin-bottom: 10px;
		font-size: 14px;
		color: #fff;
	}

	.total {
		width: 55%;
		margin-top: 30px;
		border: 1px solid #BDBDBD;
	}

	.tt {
		display: flex;
		height: 50px;
		line-height: 50px;
		font-size: 12px;
	}

	.bc {
		width: 20%;
		background-color: #dfdfdf;
	}

	.nobc {
		width: 30%;
	}
  .nobac{
    width: 80%;
  }
</style>
